<?php

namespace App\Service\Collect;


use App\Service\BaiduNlp;
use QL\QueryList;

class Google extends Base
{
    // 从百家号采集
    protected $domain;
    protected $site_id   = 10;
    protected $site_name = '谷歌新闻';

    // 初始化
    protected function init()
    {
        $this->startTime = strtotime("-3 months");

        parent::init();
    }


    // 保存到数据表
    public function saveApiDataToDb($_item, $key, $is_file = false)
    {
        // 执行采集
        $_title  = trim($_item['title']);
        $_href   = trim($_item['href']);
        $_desc   = trim($_item['describe']);
        $_html   = trim($_item['html']);
        $_time   = trim($_item['time']);
        $_text   = '';
        $_cover  = trim($_item['cover']);
        $_author = trim($_item['author']);
        $images  = [];


        $domain = 'https://google.com';
        $_href  = $this->formatUrl($domain.$_href);


        // 判断链接是否已经采集
        if($is_file){
            $info = $this->isGatherFile($_href);
        } else{
            $info = $this->isGather($_href);
        }
        if($info){
            return $this->return_false(' '.($key + 1).'：==> 已经采集文章：'.$_title);
        }
        // 如果没有标题
        /*if(empty($_title)){
            return $this->return_false(' '.($key + 1).'：==> 文章：没有标题，不能采集：'.$_title);
        }*/

        // 发布时间
        $_ctime = $this->formatTime($_time);

        // 只采集今天的
        if($this->startTime && ($_ctime < $this->startTime)){
            return $this->return_die(' '.($key + 1).'：==> 文章：是以前过期的文章，不采集：'.$_title);
        }

        // 站点信息
        $_channel  = '';
        $from_site = '谷歌新闻';
        $site_id   = 15;


        // 全匹配 精准匹配
        /*if((strpos($_desc, $this->keyword) === false) && strpos($_title, $this->keyword) === false){
            return $this->return_false('==> 文章：文章没有精准匹配到搜索的关键字！');
        }*/

        // 整理保存的数据
        $_row = [
            'channel'      => 'article',
            'link'         => $_href,
            'title'        => trim($_title),
            'description'  => $_desc,
            'text'         => $_desc,
            'time'         => $_ctime,
            'images'       => $images,
            'cover'        => $_cover,
            'author'       => $_author,
            'from_channel' => $_channel,
            'from_site'    => $from_site,
            'site_id'      => $site_id,
        ];

        //var_dump($_row);
        if($is_file){
            $res = $this->saveFile($_row);
        } else{
            $res = $this->saveArticle($_row);
        }

        return $res;
    }

    // 获取api接口的列表(测试)
    public function getApiList($keyword, $page = 1, $size = 10)
    {
        if(empty($keyword)){
            return $this->return_false('传递采集关键字！');
        }

        //   https://www.google.com.hk/search?q=Pony.ai&newwindow=1&sca_esv=b93a5106f2a6b794&tbs=sbd:1&tbm=nws&ei=yscBZ-e0H56I4-EPksbw-QQ&start=10&sa=N&ved=2ahUKEwjnwcfWrviIAxUexDgGHRIjPE8Q8NMDegQIAhAW&biw=1600&bih=839&dpr=2
        //   https://www.google.com.hk/search?q=Pony.ai&tbs=sbd:1&tbm=nws&start=30
        //   https://www.google.com.hk/search?q=易见股份&tbs=sbd:1&tbm=nws&start=$start

        // 重新获取使用
        try{
            // 百家号链接
            $start = ($page - 1) * $size;
            $url   = 'https://www.google.com.hk/search?q='.urlencode($keyword).'&tbs=sbd:1&tbm=nws&start='.$start;
            $url   = 'https://www.google.com/search?q='.urlencode($keyword).'&tbs=sbd:1&tbm=nws&start='.$start;
            $html  = $this->apiCurl()->request($url);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        // var_dump($html);
        // die;

        // 每个列表中有每个内容
        $rule = [
            'href'     => ['div div a.WlydOe', 'href'],
            'describe' => ['div div a.WlydOe div div.SoAPf div.GI74Re', 'text'],
            'author'   => ['div div a.WlydOe div div.SoAPf div.MgUUmf span', 'text'],
            'title'    => ['div div a.WlydOe div div.SoAPf div.ynAwRc', 'text'],
            'time'     => ['div div a.WlydOe div div.SoAPf div.rbYSKb span', 'text'],
            'cover'    => ['div div a.WlydOe div div.gpjNTe div div img', 'src'],
            'html'     => ['div.SoaBEf', 'html'],
        ];


        $rule = [
            'href'     => ['div a', 'href'],
            'title'    => ['div a div span.fuLhoc', 'text'],
            'author'   => ['div a div span.dXDvrc span.fYyStc', 'text'],
            'describe' => ['div a div.Dks9wf  div div span.FrIlee span:eq(0)', 'text'],
            //'describe' => ['div a div.Dks9wf table.KZhhub tbody tr td.udTCfd div div span.FrIlee span:eq(0)', 'text'],
            //'time'     => ['div a div.Dks9wf table.KZhhub tbody tr td.udTCfd div div span.FrIlee span.YVIcad', 'text'],
            'time'     => ['div a div.Dks9wf  div div span.FrIlee span.YVIcad', 'text'],
            'cover'    => ['div a div.Dks9wf  div:eq(1) img', 'src'],
            'html'     => ['div a', 'html'],
        ];

        $range = 'div.SoaBEf';
        $range = 'div.ezO2md';
        $ql    = QueryList::html($html);
        $lists = $ql->range($range)->rules($rule)->queryData();

        // 百度没有为空的时候 默认大于最后一页的翻页都是返回最后 一页的数据
        if(empty($lists)){
            return $this->return_die('谷歌新闻：没有采集内容：'.$keyword.' 当前页数：'.$page.'  每页数量：'.$size);
        }

        return $lists;
    }


    // 获取api接口的列表
    public function getApiList2($keyword, $page = 1, $size = 10)
    {
        if(empty($keyword)){
            return $this->return_false('传递采集关键字！');
        }

        // 每个列表中有每个内容
        $rule = [
            'href'     => ['div div a.WlydOe', 'href'],
            'describe' => ['div div a.WlydOe div div.SoAPf div.GI74Re', 'text'],
            'author'   => ['div div a.WlydOe div div.SoAPf div.MgUUmf span', 'text'],
            'title'    => ['div div a.WlydOe div div.SoAPf div.ynAwRc', 'text'],
            'time'     => ['div div a.WlydOe div div.SoAPf div.rbYSKb span', 'text'],
            'cover'    => ['div div a.WlydOe div div.gpjNTe div div img', 'src'],
            'html'     => ['div.SoaBEf', 'html'],
        ];

        // TODO 采集百度 必须是http 不能是https
        //   https://www.google.com.hk/search?q=Pony.ai&newwindow=1&sca_esv=b93a5106f2a6b794&tbs=sbd:1&tbm=nws&ei=yscBZ-e0H56I4-EPksbw-QQ&start=10&sa=N&ved=2ahUKEwjnwcfWrviIAxUexDgGHRIjPE8Q8NMDegQIAhAW&biw=1600&bih=839&dpr=2
        //   https://www.google.com.hk/search?q=Pony.ai&tbs=sbd:1&tbm=nws&start=30
        //   https://www.google.com.hk/search?q=易见股份&tbs=sbd:1&tbm=nws&start=$start
        try{
            // 百家号链接
            $start = ($page - 1) * $size;
            $url   = 'https://www.google.com.hk/search?q='.urlencode($keyword).'&tbs=sbd:1&tbm=nws&start='.$start;

            $lists = $this->getQLList($url, '#gsr #main #cnt #rcnt #center_col #res #search div #rso .MjjYud div', 'div.SoaBEf', $rule);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        // 百度没有为空的时候 默认大于最后一页的翻页都是返回最后 一页的数据
        if(empty($lists)){
            return $this->return_die('谷歌新闻：没有采集内容：'.$keyword.' 当前页数：'.$page.'  每页数量：'.$size);
        }

        return $lists;
    }

    // 域名解析
    protected function formatUrl($_href)
    {
        //$_href     = $domain.$_href;
        $parsedUrl = parse_url($_href);

        // 访问解析结果
        $scheme = $parsedUrl['scheme'];
        $host   = $parsedUrl['host'];
        $path   = $parsedUrl['path'];
        $query  = $parsedUrl['query'];

        // 获取参数中的url
        $_url   = '';
        $querys = explode('&', $query);
        foreach($querys as $_query){
            if(strpos($_query, 'url') === false){
                continue;
            }
            $_url = substr($_query, 4);
        }

        if(empty($_url)){
            return $_href;
        }

        return $_url;
    }

    // 时间操作
    protected function formatTime($_time)
    {
        if(empty($_time)){
            return $_time;
        }

        if(strpos($_time, 'days ago') > 0){
            $_ctime = strtotime('-'.substr($_time, 0, strpos($_time, 'days ago')).' days');
        } elseif(strpos($_time, 'day ago') > 0){
            $_ctime = strtotime('-'.substr($_time, 0, strpos($_time, 'day ago')).' days');
        } elseif(strpos($_time, 'weeks ago') > 0){
            $_ctime = strtotime('-'.substr($_time, 0, strpos($_time, 'weeks ago')).' weeks');
        } elseif(strpos($_time, 'week ago') > 0){
            $_ctime = strtotime('-'.substr($_time, 0, strpos($_time, 'week ago')).' weeks');
        } elseif(strpos($_time, 'month ago') > 0){
            $_ctime = strtotime('-'.substr($_time, 0, strpos($_time, 'month ago')).' month');
        } elseif(strpos($_time, 'months ago') > 0){
            $_ctime = strtotime('-'.substr($_time, 0, strpos($_time, 'months ago')).' month');
        } else{
            $_ctime = strtotime(str_replace(['年', '月', '日'], ['-', '-', ''], $_time));
        }

        return $_ctime;
    }
}

